import sys
import os
from read_csi_from_file import read_from_csifile
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
from mpl_toolkits.mplot3d import Axes3D

filename = "../quyuneiwuren_35s.csi"

f = read_from_csifile(filename)

def getmod(arr):
    return np.abs(arr)

def getmodl(list):
    return [getmod(arr) for arr in list]

def getphase(arr):
    return np.angle(arr)

def getphasel(list):
    return [getphase(arr) for arr in list]

# 计算相位差
def get_phase_dif(csi1,csi2):
    csi_t = [0] * 30
    for i in range(30):
        if csi1[i] >= 0 and csi2[i] >= 0:
            if csi1[i] >= csi2[i]:
                temp = csi1[i] - csi2[i]
            else:
                temp = csi2[i] - csi1[i]
        elif csi1[i] > 0 and csi2[i] < 0:
            t = csi1[i] - np.pi
            if csi2[i] > t:
                temp = np.pi - csi2[i] + t
            else:
                temp = np.pi * 2 + csi2[i] - csi1[i]
        elif csi1[i] < 0 and csi2[i] > 0:
            t = csi2[i] - np.pi
            if csi1[i] >= t:
                temp = np.pi - csi1[i] + t
            else:
                temp = np.pi * 2 + csi1[i] - csi2[i]
        else:
            if csi1[i] >= csi2[i]:
                temp = csi1[i] - csi2[i]
            else:
                temp = csi2[i] - csi1[i]

        csi_t[i] = temp

    return csi_t


# 相位扩展
def phase_expend(dat):
    csi_pha_exp = [0] * 30
    csi_pha_exp[0] = dat[0]
    for i in range(1,30):
        if (dat[i] - dat[i-1])>=np.pi:
            csi_pha_exp[i] = csi_pha_exp[i-1] + (dat[i] - dat[i-1] - 2*np.pi)
        elif (dat[i] - dat[i-1])<= -np.pi:
            csi_pha_exp[i] = csi_pha_exp[i-1] + (dat[i] - dat[i-1] + 2 * np.pi)
        else:
            csi_pha_exp[i] = csi_pha_exp[i-1] + (dat[i] - dat[i-1])
    return csi_pha_exp

# 相位线性变换
def phase_linear_trans(dat):
    csi_pha_expend = phase_expend(dat)
    csi_pha_trans = [0] * 30
    m = [-28, -26, -24, -22, -20, -18, -16, -14, -12, -10,  -8,  -6,  -4, -2, -1, 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 28]

    k = (csi_pha_expend[29] - csi_pha_expend[0])/(m[29] - m[0]) #斜率
    b = np.sum(csi_pha_expend)/30 #截距

    for i in range(30):
        csi_pha_trans[i] = csi_pha_expend[i] - k * m[i] - b

    return csi_pha_trans

# print(f[0].Ntx, f[0].Nrx)#1,2
print(len(f))
csi_amp1 = []
csi_amp2 = []
csi_pha1 = []
csi_pha2 = []
rssia = []
csi_pha_dif = []

for dat in f:
    amp1 = getmodl(dat.csi[0][0])
    if dat.Nrx==2:
        amp2 = getmodl(dat.csi[0][1])
    pha1 = phase_linear_trans(getphasel(dat.csi[0][0]))
    if dat.Nrx==2:
        pha2 = phase_linear_trans(getphasel(dat.csi[0][1]))
    rssi1 = dat.rssi_a

    if len(amp1)==30:
        csi_amp1.append(amp1)
    if len(amp2)==30:
        csi_amp2.append(amp2)
    if len(pha1)==30:
        csi_pha1.append(pha1)
    if len(pha2)==30:
        csi_pha2.append(pha2)
    rssia.append(rssi1)
# print(rssia)
# 计算相位差
    if dat.Nrx ==2:
        csi_pha_dif.append(get_phase_dif(pha1,pha2))

# z = []
# for i in csi_pha_dif:
#     z.append(i[10])

x = np.arange(1,31)
z = np.array(csi_pha1)
y = np.arange(len(z))
x,y = np.meshgrid(x,y)


print(len(x))
print(len(y))
print(len(z))

#3维视图
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x,y,z,rstride=1, cstride=1, cmap='rainbow')

# b,a = signal.butter(3,0.03,'low')
# sf = signal.filtfilt(b,a,z)

# l = len(rssia)
# s = np.arange(l)

# 2维视图
# plt.plot(y,sf,'r')
plt.show()
